<!--

    Copyright © 2016-2025 The Thingsboard Authors

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
@if (panelMode) {
  <ng-container *ngTemplateOutlet="sessionFilterPanel"></ng-container>
} @else {
  @if (buttonMode()) {
    <button color="primary"
      [matTooltip]="buttonDisplayTooltip"
      matTooltipPosition="above"
      mat-stroked-button
      (click)="toggleSessionFilterPanel($event)">
      <mat-icon>filter_list</mat-icon>{{buttonDisplayValue}}
    </button>
  } @else {
    <div class="tb-form-panel tb-session-filter-config-component no-padding no-border" [formGroup]="sessionFilterConfigForm">
      <div class="tb-form-row space-between column-xs">
        <div class="fixed-title-width" translate>mqtt-client-session.connected-status</div>
        <mat-chip-listbox multiple formControlName="connectedStatusList">
          @for (value of connectionStates; track value) {
            <mat-chip-option [value]="value">
              {{ connectionStateTranslationMap.get(value) | translate }}
            </mat-chip-option>
          }
        </mat-chip-listbox>
      </div>
      <div class="tb-form-row space-between column-xs">
        <div class="fixed-title-width" translate>mqtt-client.client-type</div>
        <mat-chip-listbox multiple formControlName="clientTypeList">
          @for (value of clientTypes; track value) {
            <mat-chip-option [value]="value">
              <span style="display: inline-flex; align-items: center; gap: 4px;">
                {{ clientTypeTranslationMap.get(value) | translate }}
                <mat-icon style="height: 20px; width: 20px; font-size: 20px;" class="material-icons mat-icon">
                  {{ clientTypeIcon.get(value) }}
                </mat-icon>
              </span>
            </mat-chip-option>
          }
        </mat-chip-listbox>
      </div>
      <div class="tb-form-row space-between column-xs">
        <div class="fixed-title-width" translate>mqtt-client-session.clean-start</div>
        <mat-chip-listbox multiple formControlName="cleanStartList">
          @for (value of cleanStartList; track value) {
            <mat-chip-option [value]="value">
              {{ value }}
            </mat-chip-option>
          }
        </mat-chip-listbox>
      </div>
      <div class="tb-form-row space-between column-xs">
        <div class="fixed-title-width" translate>mqtt-client.client-id</div>
        <mat-form-field class="flex" appearance="outline" subscriptSizing="dynamic">
          <input matInput type="text" formControlName="clientId">
        </mat-form-field>
      </div>
      <div class="tb-form-row space-between column-xs">
        <div class="fixed-title-width" translate>mqtt-client-session.client-ip</div>
        <mat-form-field class="flex" appearance="outline" subscriptSizing="dynamic">
          <input matInput type="text" formControlName="clientIpAddress">
        </mat-form-field>
      </div>
      <div class="tb-form-row column-xs">
        <div class="fixed-title-width" translate>mqtt-client-session.subscriptions</div>
        <mat-form-field appearance="outline" class="flex" subscriptSizing="dynamic">
          <mat-select formControlName="subscriptionOperation" placeholder="{{'filter.operation.operation' | translate}}">
            @for (operation of numericOperations; track operation) {
              <mat-option [value]="operation">
                <span style="font-size: 14px">{{numericOperationTranslations.get(numericOperationEnum[operation]) | translate}}</span>
              </mat-option>
            }
          </mat-select>
        </mat-form-field>
        <mat-form-field appearance="outline" class="flex number" subscriptSizing="dynamic">
          <input matInput type="number" min="0" formControlName="subscriptions">
        </mat-form-field>
      </div>
      <div class="tb-form-row column-xs">
        <div class="fixed-title-width" translate>mqtt-client-session.node-id</div>
        <tb-entity-subtype-list subscriptSizing="dynamic"
          formControlName="nodeIdList"
          appearance="outline"
          [addValueOutOfList]="false"
          [emptyInputPlaceholder]="'action.select' | translate"
          [additionalClasses]="['tb-chips', 'flex']"
          [entityType]="entityType.KAFKA_BROKER">
        </tb-entity-subtype-list>
      </div>
    </div>
  }
}
<ng-template #sessionFilterPanel>
  <form class="mat-content mat-padding flex flex-col" (ngSubmit)="update()">
    <ng-container *ngTemplateOutlet="sessionFilter"></ng-container>
    <div class="tb-panel-actions flex flex-row items-center justify-end">
      <button type="button"
        mat-button
        color="primary"
        (click)="reset()">
        {{ 'action.reset' | translate }}
      </button>
      <span class="flex flex-1"></span>
      <button type="button"
        mat-button
        (click)="cancel()">
        {{ 'action.cancel' | translate }}
      </button>
      <button type="submit"
        mat-raised-button
        color="primary"
        [disabled]="sessionFilterConfigForm.invalid || !sessionFilterConfigForm.dirty">
        {{ 'action.update' | translate }}
      </button>
    </div>
  </form>
</ng-template>
<ng-template #sessionFilter>
  <div class="tb-form-panel tb-session-filter-config-component no-padding no-border" [formGroup]="sessionFilterConfigForm">
    <div class="tb-form-row space-between column-xs">
      <div class="fixed-title-width" translate>mqtt-client-session.connected-status</div>
      <mat-chip-listbox multiple formControlName="connectedStatusList">
        @for (value of connectionStates; track value) {
          <mat-chip-option [value]="value">
            {{ connectionStateTranslationMap.get(value) | translate }}
          </mat-chip-option>
        }
      </mat-chip-listbox>
    </div>
    <div class="tb-form-row space-between column-xs">
      <div class="fixed-title-width" translate>mqtt-client.client-type</div>
      <mat-chip-listbox multiple formControlName="clientTypeList">
        @for (value of clientTypes; track value) {
          <mat-chip-option [value]="value">
            <span style="display: inline-flex; align-items: center; gap: 4px;">
              {{ clientTypeTranslationMap.get(value) | translate }}
              <mat-icon style="height: 20px; width: 20px; font-size: 20px;" class="material-icons mat-icon">
                {{ clientTypeIcon.get(value) }}
              </mat-icon>
            </span>
          </mat-chip-option>
        }
      </mat-chip-listbox>
    </div>
    <div class="tb-form-row space-between column-xs">
      <div class="fixed-title-width" translate>mqtt-client-session.clean-start</div>
      <mat-chip-listbox multiple formControlName="cleanStartList">
        @for (value of cleanStartList; track value) {
          <mat-chip-option [value]="value">
            {{ value }}
          </mat-chip-option>
        }
      </mat-chip-listbox>
    </div>
    <div class="tb-form-row space-between column-xs">
      <div class="fixed-title-width" translate>mqtt-client.client-id</div>
      <mat-form-field class="flex" appearance="outline" subscriptSizing="dynamic">
        <input matInput type="text" formControlName="clientId">
      </mat-form-field>
    </div>
    <div class="tb-form-row space-between column-xs">
      <div class="fixed-title-width" translate>mqtt-client-session.client-ip</div>
      <mat-form-field class="flex" appearance="outline" subscriptSizing="dynamic">
        <input matInput type="text" formControlName="clientIpAddress">
      </mat-form-field>
    </div>
    <div class="tb-form-row column-xs">
      <div class="fixed-title-width" translate>mqtt-client-session.subscriptions</div>
      <mat-form-field appearance="outline" class="flex" subscriptSizing="dynamic">
        <mat-select formControlName="subscriptionOperation" placeholder="{{'filter.operation.operation' | translate}}">
          @for (operation of numericOperations; track operation) {
            <mat-option [value]="operation">
              <span style="font-size: 14px">{{numericOperationTranslations.get(numericOperationEnum[operation]) | translate}}</span>
            </mat-option>
          }
        </mat-select>
      </mat-form-field>
      <mat-form-field appearance="outline" class="flex number" subscriptSizing="dynamic">
        <input matInput type="number" min="0" formControlName="subscriptions">
      </mat-form-field>
    </div>
    <div class="tb-form-row column-xs">
      <div class="fixed-title-width" translate>mqtt-client-session.node-id</div>
      <tb-entity-subtype-list subscriptSizing="dynamic"
        formControlName="nodeIdList"
        appearance="outline"
        [addValueOutOfList]="false"
        [emptyInputPlaceholder]="'action.select' | translate"
        [additionalClasses]="['tb-chips', 'flex']"
        [entityType]="entityType.KAFKA_BROKER">
      </tb-entity-subtype-list>
    </div>
  </div>
</ng-template>
